package com.fzu.mmdbproxyframework.model;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Table {
	public Integer id;
	public String name;
	public Map<String,String> table_attributes = new HashMap<String,String>();
//	public Map<String,String> columns_attributes = new HashMap<String,String>();//一个表字段的属性
	public List<Map<String,String>> columns_List = new ArrayList<Map<String,String>>();//所有表字段的属性
//	public DataNode dataNode;
	public String cache;
	/*
	 * 	System.out.println(rs2.getString("TABLE_NAME"));
			System.out.println(rs2.getString("TABLE_TYPE"));
			System.out.println(rs2.getString("ENGINE"));
			System.out.println(rs2.getString("UPDATE_TIME"));
			System.out.println(rs2.getString("TABLE_COLLATION"));
			System.out.println(rs2.getString("AUTO_INCREMENT"));
			
			
				System.out.println(rs1.getString("COLUMN_NAME"));
			System.out.println(rs1.getString("COLUMN_DEFAULT"));
			System.out.println(rs1.getString("IS_NULLABLE"));
			System.out.println(rs1.getString("CHARACTER_SET_NAME"));
			System.out.println(rs1.getString("COLUMN_TYPE"));
			System.out.println(rs1.getString("COLUMN_KEY"));
			System.out.println(rs1.getString("EXTRA"));
			System.out.println(rs1.getString("PRIVILEGES"));
			System.out.println(rs1.getString("COLUMN_COMMENT"));
			
			 /**
 	    *创建该表的sql-- Table structure for CHAPTER_INFO
	 * **/
	public String  mysql_createTableSql(){
		String createStr = "CREATE TABLE `"+table_attributes.get("TABLE_NAME")+"`(";
		for(Map<String,String> columns_attributes:columns_List){
			createStr +="`"+columns_attributes.get("COLUMN_NAME")+"` "
					+columns_attributes.get("COLUMN_TYPE");
			if(columns_attributes.get("IS_NULLABLE").equalsIgnoreCase("NO")){
				createStr += " NOT NULL "+columns_attributes.get("EXTRA")+",";				
			}else if(columns_attributes.get("IS_NULLABLE").equalsIgnoreCase("YES")){
				createStr += " default NULL "+columns_attributes.get("EXTRA")+",";
			}			
			if(columns_attributes.get("COLUMN_KEY").equalsIgnoreCase("PRI")){
				table_attributes.put("PRIMARY_KEY", columns_attributes.get("COLUMN_NAME"));
			}			
		}
		if(table_attributes.get("PRIMARY_KEY") != "" || table_attributes.get("PRIMARY_KEY") != null)//PRIMARY KEY  (`ID`)
		{
			createStr +="PRIMARY KEY  (`"+table_attributes.get("PRIMARY_KEY")+"`) )";
		}
		//ENGINE=InnoDB AUTO_INCREMENT=588 DEFAULT CHARSET=utf8;
		if(table_attributes.get("AUTO_INCREMENT") ==null || table_attributes.get("AUTO_INCREMENT").equalsIgnoreCase("")){
			createStr += "ENGINE="+table_attributes.get("ENGINE")+" ";
		}else{
			createStr += "ENGINE="+table_attributes.get("ENGINE")+" AUTO_INCREMENT="+table_attributes.get("AUTO_INCREMENT");
		}
		if(table_attributes.get("TABLE_COLLATION").contains("utf8")){
			createStr +=" DEFAULT CHARSET=utf8;";
		}
		return createStr;
	}
	
	public String createTruncateTableStr(){
		return "truncate table "+getName()+"";
	}
//	public String creatSqlStrByName(){//create table tb_1(id int,name varchar(20));
//		String tableCreatString = "create table "+this.name+"(";
//		 String column_Name = "";
//		 String column_Type = "";						
//		for(String attr:columns.keySet()){
////			System.out.println(attr+":"+columns.get(attr));
//			column_Name = attr;
//			column_Type = columns.get(attr);
//			 tableCreatString = tableCreatString + column_Name+" "+column_Type+",";
//		}
//		tableCreatString = tableCreatString.substring(0,tableCreatString.lastIndexOf(","));		  
//		tableCreatString = tableCreatString + ")";	
////		System.out.println(tableCreatString);
//		return tableCreatString;
//	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}



	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	public String getKey(){
		return this.id.toString();
	}

	public String getCache() {
		return cache;
	}

	public void setCache(String cache) {
		this.cache = cache;
	}
//
//	public DataNode getDataNode() {
//		return dataNode;
//	}
//
//	public void setDataNode(DataNode dataNode) {
//		this.dataNode = dataNode;
//	}

	public Map<String, String> getTable_attributes() {
		return table_attributes;
	}

	public void setTable_attributes(Map<String, String> table_attributes) {
		this.table_attributes = table_attributes;
	}

	public List<Map<String, String>> getColumns_List() {
		return columns_List;
	}
	public void setColumns_List(List<Map<String, String>> columns_List) {
		this.columns_List = columns_List;
	}

}
